/*
    Dao FLTK2
    Copyright (C) 2010 Belousov Oleg <belousov.oleg@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <fltk/SharedImage.h>
#include <dao.h>

#include "image.h"

static void image_new(DaoContext *ctx, DValue *p[], int N) {
    char		*file = DString_GetMBS(p[0]->v.s);
    fltk::ImageType 	*it = fltk::guess_image(file);

    if (it) {
        fltk::Image *ptr = it->get(file, 0);

        if (ptr) {
            int w = 0, h = 0;

            ptr->measure(w, h);
            DaoCData *cdata = DaoContext_PutCData(ctx, ptr, &imageTyper);
        }
    }
}

static void image_draw(DaoContext *ctx, DValue *p[], int N) {
    fltk::Image* 	ptr = (fltk::Image*) DaoCData_GetData(p[0]->v.cdata);
    int 		x = (int) p[1]->v.i;
    int 		y = (int) p[2]->v.i;

    ptr->draw(x, y);
}

static void image_w(DaoContext *ctx, DValue *p[], int N) {
    fltk::Image* 	ptr = (fltk::Image*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->w()));
}

static void image_h(DaoContext *ctx, DValue *p[], int N) {
    fltk::Image* 	ptr = (fltk::Image*) DaoCData_GetData(p[0]->v.cdata);

    DaoContext_PutInteger(ctx, (int) (ptr->h()));
}

static DaoFuncItem imageMeth[] = {
    { image_new, 		"fltk_image(file:string)" },

    { image_draw, 		"draw(self:fltk_image, x:int, y)" },
    { image_w, 			"w(self:fltk_image) => int" },
    { image_h, 			"h(self:fltk_image) => int" },

    { NULL, NULL }
};

DaoTypeBase imageTyper = {
    "fltk_image",
    NULL,
    NULL,               	/* constant */
    imageMeth,                	/* methods */
    { NULL },     		/* parent type */
    { NULL },
    NULL,
    NULL                        /* free by free() */
};
